<?php

/**
 * 收货地址
 */

namespace app\order\common;

class AddressCommon extends \app\member\common\MemberCommon {

    protected function index() {
        $this->pageInfo['name'] = '收货地址';
        $this->setMeta('收货地址');
        $this->setCrumb([
            [
                'name' => '收货地址',
                'url' => url()
            ]
        ]);
        $list = target('order/OrderAddress')->loadList([
            'user_id' => $this->userInfo['user_id']
        ], 0, 'add_id desc');
        $this->assign('pageList', $list);
        $this->memberDisplay();
    }

    protected function add() {
        if (!isPost()) {
            $this->pageInfo['name'] = '添加地址';
            $this->setMeta('添加地址');
            $this->setCrumb([
                [
                    'name' => '收货地址',
                    'url' => url()
                ]
            ]);
            $this->memberDisplay('info');
        } else {
            $data = request('post');
            if(!target('order/Address', 'service')->add($this->userInfo['user_id'], $data)) {
                $this->error(target('order/Address', 'service')->getError());
            }
            if($this->action) {
                $url = $this->action;

            }else {
                $url = url('index');
            }
            if($data['refresh']) {
                $url = '';
            }
            $this->success('收货地址添加成功!', $url);
        }
    }

    protected function edit() {
        if (!isPost()) {
            $id = request('get', 'id', 0, 'intval');
            if(empty($id)) {
                $this->error404();
            }
            $info = target('order/OrderAddress')->getWhereInfo([
                'add_id' => $id,
                'user_id' => $this->userInfo['user_id']
            ]);
            if(empty($info)) {
                $this->error404();
            }
            $this->pageInfo['name'] = '编辑地址';
            $this->setMeta('编辑地址');
            $this->setCrumb([
                [
                    'name' => '编辑地址',
                    'url' => url()
                ]
            ]);
            $this->assign('info', $info);
            $this->memberDisplay('info');
        } else {
            $data = request('post');
            if(!target('order/Address', 'service')->edit($this->userInfo['user_id'], $data)) {
                $this->error(target('order/Address', 'service')->getError());
            }
            if($this->action) {
                $url = $this->action;

            }else {
                $url = url('index');
            }
            $this->success('收货地址编辑成功!', $url);
        }
    }

    protected function del() {
        $id = request('get', 'id', 0, 'intval');
        if(empty($id)) {
            $this->error404();
        }
        if(!target('order/Address', 'service')->del($this->userInfo['user_id'], $id)) {
            $this->error(target('order/Address', 'service')->getError());
        }
        $this->success('地址删除成功!', url('index', ['action' => $this->action]));
    }

}